{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# diff\n",
    "\n",
    "result: `true/false`\n",
    "\n",
    "- `true`: The 2 objects is same.\n",
    "- `false`: The 2 objects are different\n",
    "\n",
    "\n",
    "1. If object is array / hash, all elements in array / hash will be iterated.\n",
    "2. If object can be simply compared as same, return true. (just same)\n",
    "3. Compare all instance_variables.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":diff"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load 'rpgxp-data.rb'\n",
    "\n",
    "def diff(a, b)\n",
    "    return false if a == b\n",
    "    return true if a.class != b.class    \n",
    "    if a.is_a?(Array)\n",
    "        for i in 0..[a.size, b.size].max\n",
    "            if diff(a[i], b[i])\n",
    "                p [i, a[i].class]\n",
    "                return true\n",
    "            end\n",
    "        end\n",
    "    elsif a.is_a?(Hash)\n",
    "        for i in a.keys\n",
    "            if diff(a[i], b[i])\n",
    "              p [i, a[i].class]\n",
    "              return true \n",
    "            end\n",
    "        end\n",
    "    else\n",
    "        a.instance_variables.each do |v|\n",
    "            va = a.instance_variable_get(v)\n",
    "            vb = b.instance_variable_get(v)\n",
    "            if diff(va, vb)\n",
    "                p [v, va.class]\n",
    "                return true \n",
    "            end\n",
    "        end\n",
    "    end    \n",
    "    return false\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# All data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"data/Actors.rxdata\"\n",
      "false\n",
      "\"data/Animations.rxdata\"\n",
      "[0, NilClass]\n",
      "[:@timings, Array]\n",
      "[91, RPG::Animation]\n",
      "true\n",
      "\"data/Armors.rxdata\"\n",
      "false\n",
      "\"data/Classes.rxdata\"\n",
      "false\n",
      "\"data/CommonEvents.rxdata\"\n",
      "false\n",
      "\"data/Enemies.rxdata\"\n",
      "false\n",
      "\"data/Items.rxdata\"\n",
      "false\n",
      "\"data/Map001.rxdata\"\n",
      "false\n",
      "\"data/MapInfos.rxdata\"\n",
      "false\n",
      "\"data/Skills.rxdata\"\n",
      "false\n",
      "\"data/States.rxdata\"\n",
      "false\n",
      "\"data/System.rxdata\"\n",
      "[:@_, Integer]\n",
      "true\n",
      "\"data/Tilesets.rxdata\"\n",
      "false\n",
      "\"data/Troops.rxdata\"\n",
      "false\n",
      "\"data/Weapons.rxdata\"\n",
      "false\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[\"data/Actors.rxdata\", \"data/Animations.rxdata\", \"data/Armors.rxdata\", \"data/Classes.rxdata\", \"data/CommonEvents.rxdata\", \"data/Enemies.rxdata\", \"data/Items.rxdata\", \"data/Map001.rxdata\", \"data/MapInfos.rxdata\", \"data/Scripts.rxdata\", \"data/Skills.rxdata\", \"data/States.rxdata\", \"data/System.rxdata\", \"data/Tilesets.rxdata\", \"data/Troops.rxdata\", \"data/Weapons.rxdata\"]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dir.glob('data/*.rxdata').each do |fn|\n",
    "  next if fn =~ /Scripts/  \n",
    "  a = load_data(fn)\n",
    "  b = load_data('json2' + fn)\n",
    "  p fn\n",
    "  p diff(a, b)\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "just Animations / System .rxdata is not same"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Animations.rxdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, NilClass]\n",
      "[:@timings, Array]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "91"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn = 'data/Animations.rxdata'\n",
    "a = load_data(fn)\n",
    "b = load_data('json2' + fn)\n",
    "for i in 0..a.size\n",
    "    break if diff(a[i], b[i])\n",
    "end\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "[#<RPG::Animation::Timing:0x0000000004fe14e8 @flash_color=#<Color:0x0000000004fe12e0 @red=255.0, @blue=255.0, @green=255.0, @alpha=255.0>, @condition=0, @flash_scope=0, @se=#<RPG::AudioFile:0x0000000004fe1498 @volume=80, @name=\"\", @pitch=100>, @flash_duration=5, @frame=0>]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[], [#<RPG::Animation::Timing:0x0000000004fe14e8 @flash_color=#<Color:0x0000000004fe12e0 @red=255.0, @blue=255.0, @green=255.0, @alpha=255.0>, @condition=0, @flash_scope=0, @se=#<RPG::AudioFile:0x0000000004fe1498 @volume=80, @name=\"\", @pitch=100>, @flash_duration=5, @frame=0>]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p a[91].timings, b[91].timings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The timings is an empty array."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## System.rxdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[:@_, Integer]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn = 'data/System.rxdata'\n",
    "a = load_data(fn)\n",
    "b = load_data('json2' + fn)\n",
    "diff(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7829367\n",
      "nil\n"
     ]
    }
   ],
   "source": [
    "p a.instance_variable_get(:@_)\n",
    "p b.instance_variable_get(:@_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "@_ saved with RGSS. The magic number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[:@_]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[:@_]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p a.instance_variables - b.instance_variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "@_ is not in class RPG::System, it's value is changed every save by RMXP.\n",
    "\n",
    "# Conclusion\n",
    "The translation from json to rxdata is worked."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.5.3",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
